home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / pcl / sptmbr16.lha / dfun.lisp < prev    next >
Lisp/Scheme  |  1992-12-21  |  56KB  |  1,532 lines

  1. ;;; -*- Mode:LISP; Package:PCL; Base:10; Syntax:Common-Lisp -*-
  2. ;;;
  3. ;;; *************************************************************************
  4. ;;; Copyright (c) 1985, 1986, 1987, 1988, 1989, 1990 Xerox Corporation.
  5. ;;; All rights reserved.
  6. ;;;
  7. ;;; Use and copying of this software and preparation of derivative works
  8. ;;; based upon this software are permitted.  Any distribution of this
  9. ;;; software or derivative works must comply with all applicable United
  10. ;;; States export control laws.
  11. ;;; 
  12. ;;; This software is made available AS IS, and Xerox Corporation makes no
  13. ;;; warranty about the software, its performance or its conformity to any
  14. ;;; specification.
  15. ;;; 
  16. ;;; Any person obtaining a copy of this software is requested to send their
  17. ;;; name and post office or electronic mail address to:
  18. ;;;   CommonLoops Coordinator
  19. ;;;   Xerox PARC
  20. ;;;   3333 Coyote Hill Rd.
  21. ;;;   Palo Alto, CA 94304
  22. ;;; (or send Arpanet mail to CommonLoops-Coordinator.pa@Xerox.arpa)
  23. ;;;
  24. ;;; Suggestions, comments and requests for improvements are also welcome.
  25. ;;; *************************************************************************
  26. ;;;
  27.  
  28. (in-package :pcl)
  29.  
  30. #|
  31.  
  32. This implementation of method lookup was redone in early August of 89.
  33.  
  34. It has the following properties:
  35.  
  36.  - It's modularity makes it easy to modify the actual caching algorithm.
  37.    The caching algorithm is almost completely separated into the files
  38.    cache.lisp and dlap.lisp.  This file just contains the various uses
  39.    of it. There will be more tuning as we get more results from Luis'
  40.    measurements of caching behavior.
  41.  
  42.  - The metacircularity issues have been dealt with properly.  All of
  43.    PCL now grounds out properly.  Moreover, it is now possible to have
  44.    metaobject classes which are themselves not instances of standard
  45.    metaobject classes.
  46.  
  47. ** Modularity of the code **
  48.  
  49. The actual caching algorithm is isolated in a modest number of functions.
  50. The code which generates cache lookup code is all found in cache.lisp and
  51. dlap.lisp.  Certain non-wrapper-caching special cases are in this file.
  52.  
  53.  
  54. ** Handling the metacircularity **
  55.  
  56. In CLOS, method lookup is the potential source of infinite metacircular
  57. regress.  The metaobject protocol specification gives us wide flexibility
  58. in how to address this problem.  PCL uses a technique which handles the
  59. problem not only for the metacircular language described in Chapter 3, but
  60. also for the PCL protocol which includes additional generic functions
  61. which control more aspects of the CLOS implementation.
  62.  
  63. The source of the metacircular regress can be seen in a number of ways.
  64. One is that the specified method lookup protocol must, as part of doing
  65. the method lookup (or at least the cache miss case), itself call generic
  66. functions.  It is easy to see that if the method lookup for a generic
  67. function ends up calling that same generic function there can be trouble.
  68.  
  69. Fortunately, there is an easy solution at hand.  The solution is based on 
  70. the restriction that portable code cannot change the class of a specified
  71. metaobject.  This restriction implies that for specified generic functions,
  72. the method lookup protocol they follow is fixed.  
  73.  
  74. More precisely, for such specified generic functions, most generic functions
  75. that are called during their own method lookup will not run portable methods. 
  76. This allows the implementation to usurp the actual generic function call in
  77. this case.  In short, method lookup of a standard generic function, in the
  78. case where the only applicable methods are themselves standard doesn't
  79. have to do any method lookup to implement itself.
  80.  
  81. And so, we are saved.
  82.  
  83. |#
  84.  
  85.  
  86. ;An alist in which each entry is of the form :
  87. ;  (<generator> . (<subentry> ...))
  88. ;Each subentry is of the form:
  89. ;  (<args> <constructor> <system>)
  90. (defvar *dfun-constructors* ())            
  91.  
  92. ;If this is NIL, then the whole mechanism
  93. ;for caching dfun constructors is turned
  94. ;off.  The only time that makes sense is
  95. ;when debugging LAP code. 
  96. (defvar *enable-dfun-constructor-caching* t)    
  97.  
  98. (defun show-dfun-constructors ()
  99.   (format t "~&DFUN constructor caching is ~A." 
  100.       (if *enable-dfun-constructor-caching*
  101.           "enabled" "disabled"))
  102.   (dolist (generator-entry *dfun-constructors*)
  103.     (dolist (args-entry (cdr generator-entry))
  104.       (format t "~&~S ~S"
  105.           (cons (car generator-entry) (caar args-entry))
  106.           (caddr args-entry)))))
  107.  
  108. (defvar *raise-metatypes-to-class-p* t)
  109.  
  110. (defun get-dfun-constructor (generator &rest args)
  111.   (when (and *raise-metatypes-to-class-p*
  112.          (member generator '(emit-checking emit-caching
  113.                  emit-in-checking-cache-p emit-constant-value)))
  114.     (setq args (cons (mapcar #'(lambda (mt)
  115.                  (if (eq mt 't)
  116.                      mt
  117.                      'class))
  118.                  (car args))
  119.              (cdr args))))                          
  120.   (let* ((generator-entry (assq generator *dfun-constructors*))
  121.      (args-entry (assoc args (cdr generator-entry) :test #'equal)))
  122.     (if (null *enable-dfun-constructor-caching*)
  123.     (apply (symbol-function generator) args)
  124.     (or (cadr args-entry)
  125.         (multiple-value-bind (new not-best-p)
  126.         (apply (symbol-function generator) args)
  127.           (let ((entry (list (copy-list args) new (unless not-best-p 'pcl)
  128.                  not-best-p)))
  129.         (if generator-entry
  130.             (push entry (cdr generator-entry))
  131.             (push (list generator entry)
  132.               *dfun-constructors*)))
  133.           (values new not-best-p))))))
  134.  
  135. (defun load-precompiled-dfun-constructor (generator args system constructor)
  136.   (let* ((generator-entry (assq generator *dfun-constructors*))
  137.      (args-entry (assoc args (cdr generator-entry) :test #'equal)))
  138.     (if args-entry
  139.     (when (fourth args-entry)
  140.       (let* ((dfun-type (case generator
  141.                   (emit-checking 'checking)
  142.                   (emit-caching 'caching)
  143.                   (emit-constant-value 'constant-value)
  144.                   (emit-default-only 'default-method-only)))
  145.          (metatypes (car args))
  146.          (gfs (when dfun-type (gfs-of-type dfun-type))))
  147.         (dolist (gf gfs)
  148.           (when (and (equal metatypes (arg-info-metatypes (gf-arg-info gf)))
  149.              (let ((gf-name (generic-function-name gf)))
  150.                (and (not (eq gf-name 'slot-value-using-class))
  151.                 (not (equal gf-name '(setf slot-value-using-class)))
  152.                 (not (eq gf-name 'slot-boundp-using-class)))))
  153.         (update-dfun gf)))
  154.         (setf (second args-entry) constructor)
  155.         (setf (third args-entry) system)
  156.         (setf (fourth args-entry) nil)))
  157.     (let ((entry (list args constructor system nil)))
  158.       (if generator-entry
  159.           (push entry (cdr generator-entry))
  160.           (push (list generator entry) *dfun-constructors*))))))
  161.  
  162. (defmacro precompile-dfun-constructors (&optional system)
  163.   (let ((*precompiling-lap* t))
  164.     `(progn
  165.        ,@(gathering1 (collecting)
  166.        (dolist (generator-entry *dfun-constructors*)
  167.          (dolist (args-entry (cdr generator-entry))
  168.            (when (or (null (caddr args-entry))
  169.              (eq (caddr args-entry) system))
  170.          (when system (setf (caddr args-entry) system))
  171.          (gather1
  172.            (make-top-level-form `(precompile-dfun-constructor 
  173.                       ,(car generator-entry))
  174.                     '(load)
  175.              `(load-precompiled-dfun-constructor
  176.                ',(car generator-entry)
  177.                ',(car args-entry)
  178.                ',system
  179.                ,(apply (symbol-function (car generator-entry))
  180.                    (car args-entry))))))))))))
  181.  
  182.  
  183. ;;;
  184. ;;; When all the methods of a generic function are automatically generated
  185. ;;; reader or writer methods a number of special optimizations are possible.
  186. ;;; These are important because of the large number of generic functions of
  187. ;;; this type.
  188. ;;;
  189. ;;; There are a number of cases:
  190. ;;;
  191. ;;;   ONE-CLASS-ACCESSOR
  192. ;;;     In this case, the accessor generic function has only been called
  193. ;;;     with one class of argument.  There is no cache vector, the wrapper
  194. ;;;     of the one class, and the slot index are stored directly as closure
  195. ;;;     variables of the discriminating function.  This case can convert to
  196. ;;;     either of the next kind.
  197. ;;;
  198. ;;;   TWO-CLASS-ACCESSOR
  199. ;;;     Like above, but two classes.  This is common enough to do specially.
  200. ;;;     There is no cache vector.  The two classes are stored a separate
  201. ;;;     closure variables.
  202. ;;;
  203. ;;;   ONE-INDEX-ACCESSOR
  204. ;;;     In this case, the accessor generic function has seen more than one
  205. ;;;     class of argument, but the index of the slot is the same for all
  206. ;;;     the classes that have been seen.  A cache vector is used to store
  207. ;;;     the wrappers that have been seen, the slot index is stored directly
  208. ;;;     as a closure variable of the discriminating function.  This case
  209. ;;;     can convert to the next kind.
  210. ;;;
  211. ;;;   N-N-ACCESSOR
  212. ;;;     This is the most general case.  In this case, the accessor generic
  213. ;;;     function has seen more than one class of argument and more than one
  214. ;;;     slot index.  A cache vector stores the wrappers and corresponding
  215. ;;;     slot indexes.  Because each cache line is more than one element
  216. ;;;     long, a cache lock count is used.
  217. ;;;
  218. (defstruct (dfun-info
  219.          (:constructor nil)
  220.          (:print-function print-dfun-info))
  221.   (cache nil))
  222.  
  223. (defun print-dfun-info (dfun-info stream depth)
  224.   (declare (ignore depth) (stream stream))
  225.   (printing-random-thing (dfun-info stream)
  226.     (format stream "~A" (type-of dfun-info))))
  227.  
  228. (defstruct (no-methods
  229.          (:constructor no-methods-dfun-info ())
  230.          (:include dfun-info)))
  231.  
  232. (defstruct (initial
  233.          (:constructor initial-dfun-info ())
  234.          (:include dfun-info)))
  235.  
  236. (defstruct (initial-dispatch
  237.          (:constructor initial-dispatch-dfun-info ())
  238.          (:include dfun-info)))
  239.  
  240. (defstruct (dispatch
  241.          (:constructor dispatch-dfun-info ())
  242.          (:include dfun-info)))
  243.  
  244. (defstruct (default-method-only
  245.          (:constructor default-method-only-dfun-info ())
  246.          (:include dfun-info)))
  247.  
  248. ;without caching:
  249. ;  dispatch one-class two-class default-method-only
  250.  
  251. ;with caching:
  252. ;  one-index n-n checking caching
  253.  
  254. ;accessor:
  255. ;  one-class two-class one-index n-n
  256. (defstruct (accessor-dfun-info
  257.          (:constructor nil)
  258.          (:include dfun-info))
  259.   accessor-type) ; (member reader writer)
  260.  
  261. (defmacro dfun-info-accessor-type (di)
  262.   `(accessor-dfun-info-accessor-type ,di))
  263.  
  264. (defstruct (one-index-dfun-info
  265.          (:constructor nil)
  266.          (:include accessor-dfun-info))
  267.   index)
  268.  
  269. (defmacro dfun-info-index (di)
  270.   `(one-index-dfun-info-index ,di))
  271.  
  272. (defstruct (n-n
  273.          (:constructor n-n-dfun-info (accessor-type cache))
  274.          (:include accessor-dfun-info)))
  275.  
  276. (defstruct (one-class
  277.          (:constructor one-class-dfun-info (accessor-type index wrapper0))
  278.          (:include one-index-dfun-info))
  279.   wrapper0)
  280.  
  281. (defmacro dfun-info-wrapper0 (di)
  282.   `(one-class-wrapper0 ,di))
  283.  
  284. (defstruct (two-class
  285.          (:constructor two-class-dfun-info (accessor-type index wrapper0 wrapper1))
  286.          (:include one-class))
  287.   wrapper1)
  288.  
  289. (defmacro dfun-info-wrapper1 (di)
  290.   `(two-class-wrapper1 ,di))
  291.  
  292. (defstruct (one-index
  293.          (:constructor one-index-dfun-info
  294.                (accessor-type index cache))
  295.          (:include one-index-dfun-info)))         
  296.  
  297. (defstruct (checking
  298.          (:constructor checking-dfun-info (function cache))
  299.          (:include dfun-info))
  300.   function)
  301.  
  302. (defmacro dfun-info-function (di)
  303.   `(checking-function ,di))
  304.  
  305. (defstruct (caching
  306.          (:constructor caching-dfun-info (cache))
  307.          (:include dfun-info)))
  308.  
  309. (defstruct (constant-value
  310.          (:constructor constant-value-dfun-info (cache))
  311.          (:include dfun-info)))
  312.  
  313. (defmacro dfun-update (generic-function function &rest args)
  314.   `(multiple-value-bind (dfun cache info)
  315.        (funcall ,function ,generic-function ,@args)
  316.      (update-dfun ,generic-function dfun cache info)))
  317.  
  318. (defun accessor-miss-function (gf dfun-info)
  319.   (ecase (dfun-info-accessor-type dfun-info)
  320.     (reader
  321.       #'(lambda (arg)
  322.        (declare (pcl-fast-call))
  323.        (accessor-miss gf nil arg dfun-info)))
  324.     (writer
  325.      #'(lambda (new arg)
  326.      (declare (pcl-fast-call))
  327.      (accessor-miss gf new arg dfun-info)))))
  328.  
  329. ;;;
  330. ;;; ONE-CLASS-ACCESSOR
  331. ;;;
  332. (defun make-one-class-accessor-dfun (gf type wrapper index)
  333.   (let ((emit (if (eq type 'reader) 'emit-one-class-reader 'emit-one-class-writer))
  334.     (dfun-info (one-class-dfun-info type index wrapper)))
  335.     (values
  336.      (funcall (get-dfun-constructor emit (consp index))
  337.           wrapper index
  338.           (accessor-miss-function gf dfun-info))
  339.      nil
  340.      dfun-info)))
  341.  
  342. ;;;
  343. ;;; TWO-CLASS-ACCESSOR
  344. ;;;
  345. (defun make-two-class-accessor-dfun (gf type w0 w1 index)
  346.   (let ((emit (if (eq type 'reader) 'emit-two-class-reader 'emit-two-class-writer))
  347.     (dfun-info (two-class-dfun-info type index w0 w1)))
  348.     (values
  349.      (funcall (get-dfun-constructor emit (consp index))
  350.           w0 w1 index
  351.           (accessor-miss-function gf dfun-info))
  352.      nil
  353.      dfun-info)))
  354.  
  355. ;;;
  356. ;;; std accessors same index dfun
  357. ;;;
  358. (defun make-one-index-accessor-dfun (gf type index &optional cache)
  359.   (let* ((emit (if (eq type 'reader) 'emit-one-index-readers 'emit-one-index-writers))
  360.      (cache (or cache (get-cache 1 nil #'one-index-limit-fn 4)))
  361.      (dfun-info (one-index-dfun-info type index cache)))
  362.     (declare (type cache cache))
  363.     (values
  364.      (funcall (get-dfun-constructor emit (consp index))
  365.           cache
  366.           index
  367.           (accessor-miss-function gf dfun-info))
  368.      cache
  369.      dfun-info)))
  370.  
  371. (defun make-final-one-index-accessor-dfun (gf type index table)
  372.   (let ((cache (fill-dfun-cache table nil 1 #'one-index-limit-fn)))
  373.     (make-one-index-accessor-dfun gf type index cache)))
  374.  
  375. (defun one-index-limit-fn (nlines)
  376.   (default-limit-fn nlines))
  377.  
  378.  
  379. (defun make-n-n-accessor-dfun (gf type &optional cache)
  380.   (let* ((emit (if (eq type 'reader) 'emit-n-n-readers 'emit-n-n-writers))
  381.      (cache (or cache (get-cache 1 t #'n-n-accessors-limit-fn 2)))
  382.      (dfun-info (n-n-dfun-info type cache)))
  383.     (declare (type cache cache))
  384.     (values
  385.      (funcall (get-dfun-constructor emit)
  386.           cache
  387.           (accessor-miss-function gf dfun-info))
  388.      cache
  389.      dfun-info)))
  390.  
  391. (defun make-final-n-n-accessor-dfun (gf type table)
  392.   (let ((cache (fill-dfun-cache table t 1 #'n-n-accessors-limit-fn)))
  393.     (make-n-n-accessor-dfun gf type cache)))
  394.  
  395. (defun n-n-accessors-limit-fn (nlines)
  396.   (default-limit-fn nlines))
  397.  
  398. (defun make-checking-dfun (generic-function function &optional cache)
  399.   (unless cache
  400.     (when (use-caching-dfun-p generic-function)
  401.       (return-from make-checking-dfun (make-caching-dfun generic-function)))
  402.     (when (use-dispatch-dfun-p generic-function)
  403.       (return-from make-checking-dfun (make-dispatch-dfun generic-function))))
  404.   (multiple-value-bind (nreq applyp metatypes nkeys)
  405.       (get-generic-function-info generic-function)
  406.     (declare (ignore nreq))
  407.     (if (every #'(lambda (mt) (eq mt 't)) metatypes)
  408.     (let ((dfun-info (default-method-only-dfun-info)))
  409.       (values 
  410.        (funcall (get-dfun-constructor 'emit-default-only metatypes applyp)
  411.             function)
  412.        nil
  413.        dfun-info))
  414.     (let* ((cache (or cache (get-cache nkeys nil #'checking-limit-fn 2)))
  415.            (dfun-info (checking-dfun-info function cache)))
  416.       (values
  417.        (funcall (get-dfun-constructor 'emit-checking metatypes applyp)
  418.             cache
  419.             function 
  420.             #'(lambda (&rest args)
  421.             (declare (pcl-fast-call))
  422.             #+copy-&rest-arg (setq args (copy-list args))
  423.             (checking-miss generic-function args dfun-info)))
  424.        cache
  425.        dfun-info)))))
  426.  
  427. (defun make-final-checking-dfun (generic-function function
  428.                           classes-list new-class)
  429.   (let ((metatypes (arg-info-metatypes (gf-arg-info generic-function))))
  430.     (if (every #'(lambda (mt) (eq mt 't)) metatypes)
  431.     (values #'(lambda (&rest args)
  432.             #+copy-&rest-arg (setq args (copy-list args))
  433.             (invoke-emf function args))
  434.         nil (default-method-only-dfun-info))
  435.     (let ((cache (make-final-ordinary-dfun-internal 
  436.               generic-function nil #'checking-limit-fn 
  437.               classes-list new-class)))
  438.       (make-checking-dfun generic-function function cache)))))
  439.  
  440. (defun use-default-method-only-dfun-p (generic-function)
  441.   (multiple-value-bind (nreq applyp metatypes nkeys)
  442.       (get-generic-function-info generic-function)
  443.     (declare (ignore nreq applyp nkeys))
  444.     (every #'(lambda (mt) (eq mt 't)) metatypes)))
  445.  
  446. (defun use-caching-dfun-p (generic-function)
  447.   (some #'(lambda (method)
  448.         (let ((fmf (if (listp method)
  449.                (third method)
  450.                (method-fast-function method))))
  451.           (method-function-get fmf ':slot-name-lists)))
  452.     (if (early-gf-p generic-function)
  453.         (early-gf-methods generic-function)
  454.         (generic-function-methods generic-function))))
  455.  
  456. (defun checking-limit-fn (nlines)
  457.   (default-limit-fn nlines))
  458.  
  459.  
  460. ;;;
  461. ;;;
  462. ;;;
  463. (defun make-caching-dfun (generic-function &optional cache)
  464.   (unless cache
  465.     (when (use-constant-value-dfun-p generic-function)
  466.       (return-from make-caching-dfun (make-constant-value-dfun generic-function)))
  467.     (when (use-dispatch-dfun-p generic-function)
  468.       (return-from make-caching-dfun (make-dispatch-dfun generic-function))))
  469.   (multiple-value-bind (nreq applyp metatypes nkeys)
  470.       (get-generic-function-info generic-function)
  471.     (declare (ignore nreq))
  472.     (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
  473.        (dfun-info (caching-dfun-info cache)))
  474.       (values
  475.        (funcall (get-dfun-constructor 'emit-caching metatypes applyp)
  476.         cache
  477.         #'(lambda (&rest args)
  478.             (declare (pcl-fast-call))
  479.             #+copy-&rest-arg (setq args (copy-list args))
  480.             (caching-miss generic-function args dfun-info)))
  481.        cache
  482.        dfun-info))))
  483.  
  484. (defun make-final-caching-dfun (generic-function classes-list new-class)
  485.   (let ((cache (make-final-ordinary-dfun-internal 
  486.         generic-function t #'caching-limit-fn
  487.         classes-list new-class)))
  488.     (make-caching-dfun generic-function cache)))
  489.  
  490. (defun caching-limit-fn (nlines)
  491.   (default-limit-fn nlines))
  492.  
  493. (defun insure-caching-dfun (gf)
  494.   (multiple-value-bind (nreq applyp metatypes nkeys)
  495.       (get-generic-function-info gf)
  496.     (declare (ignore nreq nkeys))
  497.     (when (and metatypes
  498.            (not (null (car metatypes)))
  499.            (dolist (mt metatypes nil)
  500.          (unless (eq mt 't) (return t))))
  501.       (get-dfun-constructor 'emit-caching metatypes applyp))))
  502.  
  503. (defun use-constant-value-dfun-p (gf &optional boolean-values-p)
  504.   (multiple-value-bind (nreq applyp metatypes nkeys)
  505.       (get-generic-function-info gf)
  506.     (declare (ignore nreq metatypes nkeys))
  507.     (let* ((early-p (early-gf-p gf))
  508.        (methods (if early-p
  509.             (early-gf-methods gf)
  510.             (generic-function-methods gf)))
  511.        (default '(unknown)))
  512.       (and (null applyp)
  513.        (or (not (eq *boot-state* 'complete))
  514.            (compute-applicable-methods-emf-std-p gf))
  515.        (notany #'(lambda (method)
  516.                (or (and (eq *boot-state* 'complete)
  517.                 (some #'eql-specializer-p
  518.                       (method-specializers method)))
  519.                (let ((value (method-function-get 
  520.                      (if early-p
  521.                          (or (third method) (second method))
  522.                          (or (method-fast-function method)
  523.                          (method-function method)))
  524.                      :constant-value default)))
  525.                  (if boolean-values-p
  526.                  (not (or (eq value 't) (eq value nil)))
  527.                  (eq value default)))))
  528.            methods)))))
  529.  
  530. (defun make-constant-value-dfun (generic-function &optional cache)
  531.   (multiple-value-bind (nreq applyp metatypes nkeys)
  532.       (get-generic-function-info generic-function)
  533.     (declare (ignore nreq applyp))
  534.     (let* ((cache (or cache (get-cache nkeys t #'caching-limit-fn 2)))
  535.        (dfun-info (constant-value-dfun-info cache)))
  536.       (values
  537.        (funcall (get-dfun-constructor 'emit-constant-value metatypes)
  538.         cache
  539.         #'(lambda (&rest args)
  540.             (declare (pcl-fast-call))
  541.             #+copy-&rest-arg (setq args (copy-list args))
  542.             (constant-value-miss generic-function args dfun-info)))
  543.        cache
  544.        dfun-info))))
  545.  
  546. (defun make-final-constant-value-dfun (generic-function classes-list new-class)
  547.   (let ((cache (make-final-ordinary-dfun-internal 
  548.         generic-function :constant-value #'caching-limit-fn
  549.         classes-list new-class)))
  550.     (make-constant-value-dfun generic-function cache)))
  551.  
  552. (defun use-dispatch-dfun-p (gf &optional (caching-p (use-caching-dfun-p gf)))
  553.   (when (eq *boot-state* 'complete)
  554.     (unless caching-p
  555.       (let* ((methods (generic-function-methods gf))
  556.          (arg-info (gf-arg-info gf))
  557.          (mt (arg-info-metatypes arg-info))
  558.          (nreq (length mt)))
  559.     ;;Is there a position at which every specializer is eql or non-standard?
  560.     (dotimes (i nreq nil)
  561.       (when (not (eq 't (nth i mt)))
  562.         (let ((some-std-class-specl-p nil))
  563.           (dolist (method methods)
  564.         (let ((specl (nth i (method-specializers method))))
  565.           (when (and (not (eql-specializer-p specl))
  566.                  (let ((sclass (specializer-class specl)))
  567.                    (or (null (class-finalized-p sclass))
  568.                    (member *the-class-standard-object*
  569.                        (class-precedence-list sclass)))))
  570.             (setq some-std-class-specl-p t))))
  571.           (unless some-std-class-specl-p
  572.         (return-from use-dispatch-dfun-p t)))))))))
  573.  
  574. (defun make-dispatch-dfun (gf)
  575.   (values (get-dispatch-function gf) nil (dispatch-dfun-info)))
  576.  
  577. (defun get-dispatch-function (gf)
  578.   (let ((methods (generic-function-methods gf)))
  579.     (function-funcall (get-secondary-dispatch-function1 gf methods nil nil nil 
  580.                             nil nil t)
  581.               nil nil)))
  582.  
  583. (defun make-final-dispatch-dfun (gf)
  584.   (make-dispatch-dfun gf))
  585.  
  586. (defun update-dispatch-dfuns ()
  587.   (dolist (gf (gfs-of-type '(dispatch initial-dispatch)))
  588.     (dfun-update gf #'make-dispatch-dfun)))
  589.  
  590. (defun fill-dfun-cache (table valuep nkeys limit-fn &optional cache)
  591.   (let ((cache (or cache (get-cache nkeys valuep limit-fn
  592.                     (+ (hash-table-count table) 3)))))
  593.     (maphash #'(lambda (classes value)
  594.          (setq cache (fill-cache cache
  595.                      (class-wrapper classes)
  596.                      value
  597.                      t)))
  598.          table)
  599.     cache))
  600.  
  601. (defun make-final-ordinary-dfun-internal (generic-function valuep limit-fn
  602.                                classes-list new-class)
  603.   (let* ((arg-info (gf-arg-info generic-function))
  604.      (nkeys (arg-info-nkeys arg-info))
  605.      (new-class (and new-class
  606.              (equal (type-of (gf-dfun-info generic-function))
  607.                 (cond ((eq valuep t) 'caching)
  608.                       ((eq valuep :constant-value) 'constant-value)
  609.                       ((null valuep) 'checking)))
  610.              new-class))
  611.      (cache (if new-class
  612.             (copy-cache (gf-dfun-cache generic-function))
  613.             (get-cache nkeys (not (null valuep)) limit-fn 4))))
  614.       (make-emf-cache generic-function valuep cache classes-list new-class)))
  615.  
  616. (defvar *dfun-miss-gfs-on-stack* ())
  617.  
  618. (defmacro dfun-miss ((gf args wrappers invalidp nemf
  619.               &optional type index caching-p applicable)
  620.              &body body)
  621.   (unless applicable (setq applicable (gensym)))
  622.   `(multiple-value-bind (,nemf ,applicable ,wrappers ,invalidp 
  623.              ,@(when type `(,type ,index)))
  624.        (cache-miss-values ,gf ,args ',(cond (caching-p 'caching)
  625.                         (type 'accessor)
  626.                         (t 'checking)))
  627.      (when (and ,applicable (not (memq ,gf *dfun-miss-gfs-on-stack*)))
  628.        (let ((*dfun-miss-gfs-on-stack* (cons ,gf *dfun-miss-gfs-on-stack*)))
  629.      ,@body))
  630.      (invoke-emf ,nemf ,args)))
  631.  
  632. ;;;
  633. ;;; The dynamically adaptive method lookup algorithm is implemented is
  634. ;;; implemented as a kind of state machine.  The kinds of discriminating
  635. ;;; function is the state, the various kinds of reasons for a cache miss
  636. ;;; are the state transitions.
  637. ;;;
  638. ;;; The code which implements the transitions is all in the miss handlers
  639. ;;; for each kind of dfun.  Those appear here.
  640. ;;;
  641. ;;; Note that within the states that cache, there are dfun updates which
  642. ;;; simply select a new cache or cache field.  Those are not considered
  643. ;;; as state transitions.
  644. ;;; 
  645. (defvar *lazy-dfun-compute-p* t)
  646. (defvar *early-p* nil)
  647.  
  648. (defun make-initial-dfun (gf)
  649.   (let ((initial-dfun 
  650.      #'(lambda (&rest args)
  651.          #+copy-&rest-arg (setq args (copy-list args))
  652.          (initial-dfun gf args))))
  653.     (multiple-value-bind (dfun cache info)
  654.     (if (and (eq *boot-state* 'complete)
  655.          (compute-applicable-methods-emf-std-p gf))
  656.         (let* ((caching-p (use-caching-dfun-p gf))
  657.            (classes-list (precompute-effective-methods 
  658.                   gf caching-p
  659.                   (not *lazy-dfun-compute-p*))))
  660.           (if *lazy-dfun-compute-p*
  661.           (cond ((use-dispatch-dfun-p gf caching-p)
  662.              (values initial-dfun nil (initial-dispatch-dfun-info)))
  663.             (caching-p
  664.              (insure-caching-dfun gf)
  665.              (values initial-dfun nil (initial-dfun-info)))
  666.             (t
  667.              (values initial-dfun nil (initial-dfun-info))))
  668.           (make-final-dfun-internal gf classes-list)))
  669.         (let ((arg-info (if (early-gf-p gf)
  670.                 (early-gf-arg-info gf)
  671.                 (gf-arg-info gf)))
  672.           (type nil))
  673.           (if (and (gf-precompute-dfun-and-emf-p arg-info)
  674.                (setq type (final-accessor-dfun-type gf)))
  675.           (if *early-p*
  676.               (values (make-early-accessor gf type) nil nil)
  677.               (make-final-accessor-dfun gf type))
  678.           (values initial-dfun nil (initial-dfun-info)))))
  679.       (set-dfun gf dfun cache info))))
  680.  
  681. (defun make-early-accessor (gf type)
  682.   (let* ((methods (early-gf-methods gf))
  683.      (slot-name (early-method-standard-accessor-slot-name (car methods))))
  684.     (ecase type
  685.       (reader #'(lambda (instance)
  686.           (let* ((class (class-of instance))
  687.              (class-name (bootstrap-get-slot 'class class 'name)))
  688.             (bootstrap-get-slot class-name instance slot-name))))
  689.       (writer #'(lambda (new-value instance)
  690.           (let* ((class (class-of instance))
  691.              (class-name (bootstrap-get-slot 'class class 'name)))
  692.             (bootstrap-set-slot class-name instance slot-name new-value)))))))
  693.  
  694. (defun initial-dfun (gf args)
  695.   (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
  696.     (cond (invalidp)
  697.       ((and ntype nindex)
  698.        (dfun-update 
  699.         gf #'make-one-class-accessor-dfun ntype wrappers nindex))
  700.       ((use-caching-dfun-p gf)
  701.        (dfun-update gf #'make-caching-dfun))
  702.       (t
  703.        (dfun-update 
  704.         gf #'make-checking-dfun
  705.         ;; nemf is suitable only for caching, have to do this:
  706.         (cache-miss-values gf args 'checking))))))
  707.  
  708. (defun make-final-dfun (gf &optional classes-list)
  709.   (multiple-value-bind (dfun cache info)
  710.       (make-final-dfun-internal gf classes-list)
  711.     (set-dfun gf dfun cache info)))
  712.  
  713. (defvar *new-class* nil)
  714.  
  715. (defvar *free-hash-tables* (mapcar #'list '(eq equal eql)))
  716.  
  717. (defmacro with-hash-table ((table test) &body forms)
  718.   `(let* ((.free. (assoc ',test *free-hash-tables*))
  719.       (,table (if (cdr .free.)
  720.               (pop (cdr .free.))
  721.               (make-hash-table :test ',test))))
  722.      (multiple-value-prog1
  723.      (progn ,@forms)
  724.        (clrhash ,table)
  725.        (push ,table (cdr .free.)))))
  726.  
  727. (defmacro with-eq-hash-table ((table) &body forms)
  728.   `(with-hash-table (,table eq) ,@forms))
  729.  
  730. (defun final-accessor-dfun-type (gf)
  731.   (let ((methods (if (early-gf-p gf)
  732.              (early-gf-methods gf)
  733.              (generic-function-methods gf))))
  734.     (cond ((every #'(lambda (method) 
  735.               (if (consp method)
  736.               (eq *the-class-standard-reader-method*
  737.                   (early-method-class method))
  738.               (standard-reader-method-p method)))
  739.           methods)
  740.        'reader)
  741.       ((every #'(lambda (method) 
  742.               (if (consp method)
  743.               (eq *the-class-standard-writer-method*
  744.                   (early-method-class method))
  745.               (standard-writer-method-p method)))
  746.           methods)
  747.        'writer))))
  748.  
  749. (defun make-final-accessor-dfun (gf type &optional classes-list new-class)
  750.   (with-eq-hash-table (table)
  751.     (multiple-value-bind (table all-index first second size no-class-slots-p)
  752.     (make-accessor-table gf type table)
  753.       (if table
  754.       (cond ((= size 1)
  755.          (let ((w (class-wrapper first)))
  756.            (make-one-class-accessor-dfun gf type w all-index)))
  757.         ((and (= size 2) (or (integerp all-index) (consp all-index)))
  758.          (let ((w0 (class-wrapper first))
  759.                (w1 (class-wrapper second)))
  760.            (make-two-class-accessor-dfun gf type w0 w1 all-index)))
  761.         ((or (integerp all-index) (consp all-index))
  762.          (make-final-one-index-accessor-dfun 
  763.           gf type all-index table))
  764.         (no-class-slots-p
  765.          (make-final-n-n-accessor-dfun gf type table))
  766.         (t
  767.          (make-final-caching-dfun gf classes-list new-class)))
  768.       (make-final-caching-dfun gf classes-list new-class)))))
  769.  
  770. (defun make-final-dfun-internal (gf &optional classes-list)
  771.   (let ((methods (generic-function-methods gf)) type
  772.     (new-class *new-class*) (*new-class* nil)
  773.     specls all-same-p)
  774.     (cond ((null methods)
  775.        (values
  776.         #'(lambda (&rest args)
  777.         (apply #'no-applicable-method gf args))
  778.         nil
  779.         (no-methods-dfun-info)))
  780.       ((setq type (final-accessor-dfun-type gf))
  781.        (make-final-accessor-dfun gf type classes-list new-class))
  782.       ((and (not (and (every #'(lambda (specl) (eq specl *the-class-t*))
  783.                  (setq specls (method-specializers (car methods))))
  784.               (setq all-same-p
  785.                 (every #'(lambda (method)
  786.                        (and (equal specls
  787.                                (method-specializers method))))
  788.                        methods))))
  789.         (use-constant-value-dfun-p gf))
  790.        (make-final-constant-value-dfun gf classes-list new-class))
  791.       ((use-dispatch-dfun-p gf)
  792.        (make-final-dispatch-dfun gf))
  793.       ((and all-same-p (not (use-caching-dfun-p gf)))
  794.        (let ((emf (get-secondary-dispatch-function gf methods nil)))
  795.          (make-final-checking-dfun gf emf classes-list new-class)))
  796.       (t
  797.        (make-final-caching-dfun gf classes-list new-class)))))
  798.  
  799. (defun accessor-miss (gf new object dfun-info)
  800.   (let* ((ostate (type-of dfun-info))
  801.      (otype (dfun-info-accessor-type dfun-info))
  802.      oindex ow0 ow1 cache
  803.      (args (ecase otype            ;The congruence rules assure
  804.         (reader (list object))        ;us that this is safe despite
  805.         (writer (list new object)))))    ;not knowing the new type yet.
  806.     (dfun-miss (gf args wrappers invalidp nemf ntype nindex)
  807.       ;;
  808.       ;; The following lexical functions change the state of the
  809.       ;; dfun to that which is their name.  They accept arguments
  810.       ;; which are the parameters of the new state, and get other
  811.       ;; information from the lexical variables bound above.
  812.       ;; 
  813.       (flet ((two-class (index w0 w1)
  814.            (when (zerop (random 2)) (psetf w0 w1 w1 w0))
  815.            (dfun-update gf #'make-two-class-accessor-dfun ntype w0 w1 index))
  816.          (one-index (index &optional cache)
  817.            (dfun-update gf #'make-one-index-accessor-dfun ntype index cache))
  818.          (n-n (&optional cache)
  819.            (if (consp nindex)
  820.            (dfun-update gf #'make-checking-dfun nemf)
  821.            (dfun-update gf #'make-n-n-accessor-dfun ntype cache)))
  822.          (caching () ; because cached accessor emfs are much faster for accessors
  823.            (dfun-update gf #'make-caching-dfun))
  824.          ;;
  825.          (do-fill (update-fn)
  826.            (let ((ncache (fill-cache cache wrappers nindex)))
  827.          (unless (eq ncache cache)
  828.            (funcall update-fn ncache)))))
  829.     (cond ((null ntype)
  830.            (caching))
  831.           ((or invalidp
  832.            (null nindex)))
  833.           ((not (or (std-instance-p object)
  834.             (fsc-instance-p object)))
  835.            (caching))
  836.           ((or (neq ntype otype) (listp wrappers))
  837.            (caching))
  838.           (t
  839.            (ecase ostate
  840.          (one-class
  841.           (setq oindex (dfun-info-index dfun-info))
  842.           (setq ow0 (dfun-info-wrapper0 dfun-info))
  843.           (unless (eq ow0 wrappers)
  844.             (if (eql nindex oindex)
  845.             (two-class nindex ow0 wrappers)
  846.             (n-n))))
  847.          (two-class
  848.           (setq oindex (dfun-info-index dfun-info))
  849.           (setq ow0 (dfun-info-wrapper0 dfun-info))
  850.           (setq ow1 (dfun-info-wrapper1 dfun-info))
  851.           (unless (or (eq ow0 wrappers) (eq ow1 wrappers))
  852.             (if (eql nindex oindex)
  853.             (one-index nindex)
  854.             (n-n))))
  855.          (one-index
  856.           (setq oindex (dfun-info-index dfun-info))
  857.           (setq cache (dfun-info-cache dfun-info))
  858.           (if (eql nindex oindex)
  859.               (do-fill #'(lambda (ncache)
  860.                    (one-index nindex ncache)))
  861.               (n-n)))
  862.          (n-n
  863.           (setq cache (dfun-info-cache dfun-info))
  864.           (if (consp nindex)
  865.               (caching)
  866.               (do-fill #'n-n))))))))))
  867.  
  868. (defun checking-miss (generic-function args dfun-info)
  869.   (let ((oemf (dfun-info-function dfun-info))
  870.     (cache (dfun-info-cache dfun-info)))
  871.     (dfun-miss (generic-function args wrappers invalidp nemf)
  872.       (cond (invalidp)
  873.         ((eq oemf nemf)
  874.          (let ((ncache (fill-cache cache wrappers nil)))
  875.            (unless (eq ncache cache)
  876.          (dfun-update generic-function #'make-checking-dfun 
  877.                   nemf ncache))))
  878.         (t
  879.          (dfun-update generic-function #'make-caching-dfun))))))
  880.  
  881. (defun caching-miss (generic-function args dfun-info)
  882.   (let ((ocache (dfun-info-cache dfun-info)))
  883.     (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
  884.       (cond (invalidp)
  885.         (t
  886.          (let ((ncache (fill-cache ocache wrappers emf)))
  887.            (unless (eq ncache ocache)
  888.          (dfun-update generic-function 
  889.                   #'make-caching-dfun ncache))))))))
  890.  
  891. (defun constant-value-miss (generic-function args dfun-info)
  892.   (let ((ocache (dfun-info-cache dfun-info)))
  893.     (dfun-miss (generic-function args wrappers invalidp emf nil nil t)
  894.       (cond (invalidp)
  895.         (t
  896.          (let* ((function (typecase emf
  897.                 (fast-method-call (fast-method-call-function emf))
  898.                 (method-call (method-call-function emf))))
  899.             (value (method-function-get function :constant-value))
  900.             (ncache (fill-cache ocache wrappers value)))
  901.            (unless (eq ncache ocache)
  902.          (dfun-update generic-function
  903.                   #'make-constant-value-dfun ncache))))))))
  904.  
  905. ;;; Given a generic function and a set of arguments to that generic function,
  906. ;;; returns a mess of values.
  907. ;;;
  908. ;;;  <function>   The compiled effective method function for this set of
  909. ;;;               arguments. 
  910. ;;;
  911. ;;;  <applicable> Sorted list of applicable methods. 
  912. ;;;
  913. ;;;  <wrappers>   Is a single wrapper if the generic function has only
  914. ;;;               one key, that is arg-info-nkeys of the arg-info is 1.
  915. ;;;               Otherwise a list of the wrappers of the specialized
  916. ;;;               arguments to the generic function.
  917. ;;;
  918. ;;;               Note that all these wrappers are valid.  This function
  919. ;;;               does invalid wrapper traps when it finds an invalid
  920. ;;;               wrapper and then returns the new, valid wrapper.
  921. ;;;
  922. ;;;  <invalidp>   True if any of the specialized arguments had an invalid
  923. ;;;               wrapper, false otherwise.
  924. ;;;
  925. ;;;  <type>       READER or WRITER when the only method that would be run
  926. ;;;               is a standard reader or writer method.  To be specific,
  927. ;;;               the value is READER when the method combination is eq to
  928. ;;;               *standard-method-combination*; there are no applicable
  929. ;;;               :before, :after or :around methods; and the most specific
  930. ;;;               primary method is a standard reader method.
  931. ;;;
  932. ;;;  <index>      If <type> is READER or WRITER, and the slot accessed is
  933. ;;;               an :instance slot, this is the index number of that slot
  934. ;;;               in the object argument.
  935. ;;;
  936. (defun cache-miss-values (gf args state)
  937.   (if (null (if (early-gf-p gf)
  938.         (early-gf-methods gf)
  939.         (generic-function-methods gf)))
  940.       (apply #'no-applicable-method gf args)
  941.       (multiple-value-bind (nreq applyp metatypes nkeys arg-info)
  942.       (get-generic-function-info gf)
  943.     (declare (ignore nreq applyp nkeys))
  944.     (with-dfun-wrappers (args metatypes)
  945.       (dfun-wrappers invalid-wrapper-p wrappers classes types)
  946.       (error "The function ~S requires at least ~D arguments"
  947.          gf (length metatypes))
  948.       (multiple-value-bind (emf methods accessor-type index)
  949.           (cache-miss-values-internal gf arg-info wrappers classes types state)
  950.         (values emf methods
  951.             dfun-wrappers
  952.             invalid-wrapper-p
  953.             accessor-type index))))))
  954.  
  955. (defun cache-miss-values-internal (gf arg-info wrappers classes types state)
  956.   (let* ((for-accessor-p (eq state 'accessor))
  957.      (for-cache-p (or (eq state 'caching) (eq state 'accessor)))
  958.      (cam-std-p (or (null arg-info)
  959.             (gf-info-c-a-m-emf-std-p arg-info))))
  960.     (multiple-value-bind (methods all-applicable-and-sorted-p)
  961.     (if cam-std-p
  962.         (compute-applicable-methods-using-types gf types)
  963.         (compute-applicable-methods-using-classes gf classes))
  964.       (let ((emf (if (or cam-std-p all-applicable-and-sorted-p)
  965.              (function-funcall (get-secondary-dispatch-function1
  966.                     gf methods types nil (and for-cache-p wrappers)
  967.                     all-applicable-and-sorted-p)
  968.                        nil (and for-cache-p wrappers))
  969.              (default-secondary-dispatch-function gf))))
  970.     (multiple-value-bind (index accessor-type)
  971.         (and for-accessor-p all-applicable-and-sorted-p methods
  972.          (accessor-values gf arg-info classes methods))
  973.       (values (if (integerp index) index emf)
  974.           methods accessor-type index))))))
  975.  
  976. (defun accessor-values (gf arg-info classes methods)
  977.   (declare (ignore gf))
  978.   (let* ((accessor-type (gf-info-simple-accessor-type arg-info))
  979.      (accessor-class (case accessor-type
  980.                (reader (car classes))
  981.                (writer (cadr classes))
  982.                (boundp (car classes)))))
  983.     (accessor-values-internal accessor-type accessor-class methods)))
  984.  
  985. (defun accessor-values1 (gf accessor-type accessor-class)
  986.   (let* ((type `(class-eq ,accessor-class))
  987.      (types (if (eq accessor-type 'writer) `(t ,type) `(,type)))
  988.      (methods (compute-applicable-methods-using-types gf types)))
  989.     (accessor-values-internal accessor-type accessor-class methods)))
  990.  
  991. (defun accessor-values-internal (accessor-type accessor-class methods)
  992.   (let* ((meth (car methods))
  993.      (early-p (not (eq *boot-state* 'complete)))
  994.      (slot-name (when accessor-class
  995.               (if (consp meth)
  996.               (and (early-method-standard-accessor-p meth)
  997.                    (early-method-standard-accessor-slot-name meth))
  998.               (and (member *the-class-standard-object*
  999.                        (if early-p
  1000.                        (early-class-precedence-list accessor-class)
  1001.                        (class-precedence-list accessor-class)))
  1002.                    (if early-p
  1003.                    (not (eq *the-class-standard-method*
  1004.                         (early-method-class meth)))
  1005.                    (standard-accessor-method-p meth))
  1006.                    (if early-p
  1007.                    (early-accessor-method-slot-name meth)
  1008.                    (accessor-method-slot-name meth))))))
  1009.      (slotd (and accessor-class
  1010.              (if early-p
  1011.              (dolist (slot (early-class-slotds accessor-class) nil)
  1012.                (when (eql slot-name (early-slot-definition-name slot))
  1013.                  (return slot)))
  1014.              (find-slot-definition accessor-class slot-name)))))
  1015.     (when (and slotd
  1016.            (or early-p
  1017.            (slot-accessor-std-p slotd accessor-type)))
  1018.       (values (if early-p
  1019.           (early-slot-definition-location slotd) 
  1020.           (slot-definition-location slotd))
  1021.           accessor-type))))
  1022.  
  1023. (defun make-accessor-table (gf type &optional table)
  1024.   (unless table (setq table (make-hash-table :test 'eq)))
  1025.   (let ((methods (if (early-gf-p gf)
  1026.              (early-gf-methods gf)
  1027.              (generic-function-methods gf)))
  1028.     (all-index nil)
  1029.     (no-class-slots-p t)
  1030.     (early-p (not (eq *boot-state* 'complete)))
  1031.     first second (size 0))
  1032.     (declare (fixnum size))
  1033.     ;; class -> {(specl slotd)}
  1034.     (dolist (method methods)
  1035.       (let* ((specializers (if (consp method)
  1036.                    (early-method-specializers method t)
  1037.                    (method-specializers method)))
  1038.          (specl (if (eq type 'reader)
  1039.             (car specializers)
  1040.             (cadr specializers)))
  1041.          (specl-cpl (if early-p
  1042.                 (early-class-precedence-list specl)
  1043.                 (and (class-finalized-p specl)
  1044.                  (class-precedence-list specl))))
  1045.          (so-p (member *the-class-standard-object* specl-cpl))
  1046.          (slot-name (if (consp method)
  1047.                 (and (early-method-standard-accessor-p method)
  1048.                  (early-method-standard-accessor-slot-name method))
  1049.                 (accessor-method-slot-name method))))
  1050.     (when (or (null specl-cpl)
  1051.           (member *the-class-structure-object* specl-cpl))
  1052.       (return-from make-accessor-table nil))
  1053.     (maphash #'(lambda (class slotd)
  1054.              (let ((cpl (if early-p
  1055.                     (early-class-precedence-list class)
  1056.                     (class-precedence-list class))))
  1057.                (when (memq specl cpl)
  1058.              (unless (and (or so-p
  1059.                       (member *the-class-standard-object* cpl))
  1060.                       (or early-p
  1061.                       (slot-accessor-std-p slotd type)))
  1062.                (return-from make-accessor-table nil))
  1063.              (push (cons specl slotd) (gethash class table)))))
  1064.          (gethash slot-name *name->class->slotd-table*))))
  1065.     (maphash #'(lambda (class specl+slotd-list)
  1066.          (dolist (sclass (if early-p
  1067.                     (early-class-precedence-list class)
  1068.                     (class-precedence-list class)) 
  1069.               (error "This can't happen"))
  1070.            (let ((a (assq sclass specl+slotd-list)))
  1071.              (when a
  1072.                (let* ((slotd (cdr a))
  1073.                   (index (if early-p
  1074.                      (early-slot-definition-location slotd) 
  1075.                      (slot-definition-location slotd))))
  1076.              (unless index (return-from make-accessor-table nil))
  1077.              (setf (gethash class table) index)
  1078.              (when (consp index) (setq no-class-slots-p nil))
  1079.              (setq all-index (if (or (null all-index)
  1080.                          (eql all-index index))
  1081.                          index t))
  1082.              (incf size)
  1083.              (cond ((= size 1) (setq first class))
  1084.                    ((= size 2) (setq second class)))
  1085.              (return nil))))))
  1086.          table)
  1087.     (values table all-index first second size no-class-slots-p)))
  1088.  
  1089. (defun compute-applicable-methods-using-types (generic-function types)
  1090.   (let ((definite-p t) (possibly-applicable-methods nil))
  1091.     (dolist (method (if (early-gf-p generic-function)
  1092.             (early-gf-methods generic-function)
  1093.             (generic-function-methods generic-function)))
  1094.       (let ((specls (if (consp method)
  1095.             (early-method-specializers method t)
  1096.             (method-specializers method)))
  1097.         (types types)
  1098.         (possibly-applicable-p t) (applicable-p t))
  1099.     (dolist (specl specls)
  1100.       (multiple-value-bind (specl-applicable-p specl-possibly-applicable-p)
  1101.           (specializer-applicable-using-type-p specl (pop types))
  1102.         (unless specl-applicable-p
  1103.           (setq applicable-p nil))
  1104.         (unless specl-possibly-applicable-p
  1105.           (setq possibly-applicable-p nil)
  1106.           (return nil))))
  1107.     (when possibly-applicable-p
  1108.       (unless applicable-p (setq definite-p nil))
  1109.       (push method possibly-applicable-methods))))
  1110.     (let ((precedence (arg-info-precedence (if (early-gf-p generic-function)
  1111.                            (early-gf-arg-info generic-function)
  1112.                            (gf-arg-info generic-function)))))
  1113.       (values (sort-applicable-methods precedence
  1114.                        (nreverse possibly-applicable-methods)
  1115.                        types)
  1116.           definite-p))))
  1117.  
  1118. (defun sort-applicable-methods (precedence methods types)
  1119.   (sort-methods methods
  1120.         precedence
  1121.         #'(lambda (class1 class2 index)
  1122.             (let* ((class (type-class (nth index types)))
  1123.                (cpl (if (eq *boot-state* 'complete)
  1124.                     (class-precedence-list class)
  1125.                     (early-class-precedence-list class))))
  1126.               (if (memq class2 (memq class1 cpl))
  1127.               class1 class2)))))
  1128.  
  1129. (defun sort-methods (methods precedence compare-classes-function)
  1130.   (flet ((sorter (method1 method2)
  1131.        (dolist (index precedence)
  1132.          (let* ((specl1 (nth index (if (listp method1)
  1133.                        (early-method-specializers method1 t)
  1134.                        (method-specializers method1))))
  1135.             (specl2 (nth index (if (listp method2)
  1136.                        (early-method-specializers method2 t)
  1137.                        (method-specializers method2))))
  1138.             (order (order-specializers
  1139.                  specl1 specl2 index compare-classes-function)))
  1140.            (when order
  1141.          (return-from sorter (eq order specl1)))))))
  1142.     (stable-sort methods #'sorter)))
  1143.  
  1144. (defun order-specializers (specl1 specl2 index compare-classes-function)
  1145.   (let ((type1 (if (eq *boot-state* 'complete)
  1146.            (specializer-type specl1)
  1147.            (bootstrap-get-slot 'specializer specl1 'type)))
  1148.     (type2 (if (eq *boot-state* 'complete)
  1149.            (specializer-type specl2)
  1150.            (bootstrap-get-slot 'specializer specl2 'type))))
  1151.     (cond ((eq specl1 specl2)
  1152.        nil)
  1153.       ((atom type1)
  1154.        specl2)
  1155.       ((atom type2)
  1156.        specl1)
  1157.       (t
  1158.        (case (car type1)
  1159.          (class    (case (car type2)
  1160.              (class (funcall compare-classes-function specl1 specl2 index))
  1161.              (t specl2)))
  1162.          (prototype (case (car type2)
  1163.              (class (funcall compare-classes-function specl1 specl2 index))
  1164.              (t specl2)))
  1165.          (class-eq (case (car type2)
  1166.              (eql specl2)
  1167.              (class-eq nil)
  1168.              (class type1)))
  1169.          (eql      (case (car type2)
  1170.              (eql nil)
  1171.              (t specl1))))))))
  1172.  
  1173. (defun map-all-orders (methods precedence function)
  1174.   (let ((choices nil))
  1175.     (flet ((compare-classes-function (class1 class2 index)
  1176.          (declare (ignore index))
  1177.          (let ((choice nil))
  1178.            (dolist (c choices nil)
  1179.          (when (or (and (eq (first c) class1)
  1180.                 (eq (second c) class2))
  1181.                (and (eq (first c) class2)
  1182.                 (eq (second c) class1)))
  1183.            (return (setq choice c))))
  1184.            (unless choice
  1185.          (setq choice
  1186.                (if (class-might-precede-p class1 class2)
  1187.                (if (class-might-precede-p class2 class1)
  1188.                    (list class1 class2 nil t)
  1189.                    (list class1 class2 t))
  1190.                (if (class-might-precede-p class2 class1)
  1191.                    (list class2 class1 t)
  1192.                    (let ((name1 (class-name class1))
  1193.                      (name2 (class-name class2)))
  1194.                  (if (and name1 name2 (symbolp name1) (symbolp name2)
  1195.                       (string< (symbol-name name1)
  1196.                            (symbol-name name2)))
  1197.                      (list class1 class2 t)
  1198.                      (list class2 class1 t))))))
  1199.          (push choice choices))
  1200.            (car choice))))
  1201.       (loop (funcall function
  1202.              (sort-methods methods precedence #'compare-classes-function))
  1203.         (unless (dolist (c choices nil)
  1204.               (unless (third c)
  1205.             (rotatef (car c) (cadr c))
  1206.             (return (setf (third c) t))))
  1207.           (return nil))))))
  1208.  
  1209. (defvar *in-precompute-effective-methods-p* nil)
  1210.  
  1211. ;used only in map-all-orders
  1212. (defun class-might-precede-p (class1 class2)
  1213.   (if (not *in-precompute-effective-methods-p*)
  1214.       (not (member class1 (cdr (class-precedence-list class2))))
  1215.       (class-can-precede-p class1 class2)))
  1216.  
  1217. (defun compute-precedence (lambda-list nreq argument-precedence-order)
  1218.   (if (null argument-precedence-order)
  1219.       (let ((list nil))(dotimes (i nreq list) (push (- (1- nreq) i) list)))
  1220.       (mapcar #'(lambda (x) (position x lambda-list)) argument-precedence-order)))
  1221.  
  1222. (defun saut-and (specl type)
  1223.   (let ((applicable nil)
  1224.     (possibly-applicable t))
  1225.     (dolist (type (cdr type))
  1226.       (multiple-value-bind (appl poss-appl)
  1227.       (specializer-applicable-using-type-p specl type)
  1228.     (when appl (return (setq applicable t)))
  1229.     (unless poss-appl (return (setq possibly-applicable nil)))))
  1230.     (values applicable possibly-applicable)))
  1231.  
  1232. (defun saut-not (specl type)
  1233.   (let ((ntype (cadr type)))
  1234.     (values nil
  1235.         (case (car ntype)
  1236.           (class      (saut-not-class specl ntype))
  1237.           (class-eq   (saut-not-class-eq specl ntype))
  1238.           (prototype  (saut-not-prototype specl ntype))
  1239.           (eql        (saut-not-eql specl ntype))
  1240.           (t (error "~s cannot handle the second argument ~s"
  1241.             'specializer-applicable-using-type-p type))))))
  1242.  
  1243. (defun saut-not-class (specl ntype)
  1244.   (let* ((class (type-class specl))
  1245.      (cpl (class-precedence-list class)))
  1246.      (not (memq (cadr ntype) cpl))))
  1247.  
  1248. (defun saut-not-prototype (specl ntype)
  1249.   (let* ((class (case (car specl)
  1250.           (eql       (class-of (cadr specl)))
  1251.           (class-eq  (cadr specl))
  1252.           (prototype (cadr specl))
  1253.           (class     (cadr specl))))
  1254.      (cpl (class-precedence-list class)))
  1255.      (not (memq (cadr ntype) cpl))))
  1256.  
  1257. (defun saut-not-class-eq (specl ntype)
  1258.   (let ((class (case (car specl)
  1259.          (eql      (class-of (cadr specl)))
  1260.          (class-eq (cadr specl)))))
  1261.     (not (eq class (cadr ntype)))))
  1262.  
  1263. (defun saut-not-eql (specl ntype)
  1264.   (case (car specl)
  1265.     (eql (not (eql (cadr specl) (cadr ntype))))
  1266.     (t   t)))
  1267.  
  1268. (defun class-applicable-using-class-p (specl type)
  1269.   (let ((pred (memq specl (if (eq *boot-state* 'complete)
  1270.                   (class-precedence-list type)
  1271.                   (early-class-precedence-list type)))))
  1272.     (values pred
  1273.         (or pred
  1274.         (if (not *in-precompute-effective-methods-p*)
  1275.             ;; classes might get common subclass
  1276.             (superclasses-compatible-p specl type)
  1277.             ;; worry only about existing classes
  1278.             (classes-have-common-subclass-p specl type))))))
  1279.  
  1280. (defun classes-have-common-subclass-p (class1 class2)
  1281.   (or (eq class1 class2)
  1282.       (let ((class1-subs (class-direct-subclasses class1)))
  1283.     (or (memq class2 class1-subs)
  1284.         (dolist (class1-sub class1-subs nil)
  1285.           (when (classes-have-common-subclass-p class1-sub class2)
  1286.         (return t)))))))
  1287.  
  1288. (defun saut-class (specl type)
  1289.   (case (car specl)
  1290.     (class (class-applicable-using-class-p (cadr specl) (cadr type)))
  1291.     (t     (values nil (let ((class (type-class specl)))
  1292.              (memq (cadr type)
  1293.                    (class-precedence-list class)))))))
  1294.  
  1295. (defun saut-class-eq (specl type)
  1296.   (if (eq (car specl) 'eql)
  1297.       (values nil (eq (class-of (cadr specl)) (cadr type)))
  1298.       (let ((pred (case (car specl)
  1299.             (class-eq   
  1300.              (eq (cadr specl) (cadr type)))
  1301.             (class      
  1302.              (or (eq (cadr specl) (cadr type))
  1303.              (memq (cadr specl)
  1304.                    (if (eq *boot-state* 'complete)
  1305.                    (class-precedence-list (cadr type))
  1306.                    (early-class-precedence-list (cadr type)))))))))
  1307.     (values pred pred))))
  1308.  
  1309. (defun saut-prototype (specl type)
  1310.   (declare (ignore specl type))
  1311.   (values nil nil)) ; fix this someday
  1312.  
  1313. (defun saut-eql (specl type) 
  1314.   (let ((pred (case (car specl)
  1315.         (eql        (eql (cadr specl) (cadr type)))
  1316.         (class-eq   (eq (cadr specl) (class-of (cadr type))))
  1317.         (class      (memq (cadr specl)
  1318.                   (let ((class (class-of (cadr type))))
  1319.                     (if (eq *boot-state* 'complete)
  1320.                     (class-precedence-list class)
  1321.                     (early-class-precedence-list class))))))))
  1322.     (values pred pred)))
  1323.  
  1324. (defun specializer-applicable-using-type-p (specl type)
  1325.   (setq specl (type-from-specializer specl))
  1326.   (when (eq specl 't)
  1327.     (return-from specializer-applicable-using-type-p (values t t)))
  1328.   ;; This is used by c-a-m-u-t and generate-discrimination-net-internal,
  1329.   ;; and has only what they need.
  1330.   (if (or (atom type) (eq (car type) 't))
  1331.       (values nil t)
  1332.       (case (car type)
  1333.     (and        (saut-and specl type))
  1334.     (not        (saut-not specl type))
  1335.     (class      (saut-class specl type))
  1336.     (prototype  (saut-prototype specl type))
  1337.     (class-eq   (saut-class-eq specl type))
  1338.     (eql        (saut-eql specl type))
  1339.     (t          (error "~s cannot handle the second argument ~s"
  1340.                'specializer-applicable-using-type-p type)))))
  1341.  
  1342. (defun map-all-classes (function &optional (root 't))
  1343.   (let ((braid-p (or (eq *boot-state* 'braid)
  1344.              (eq *boot-state* 'complete))))
  1345.     (labels ((do-class (class)
  1346.            (mapc #'do-class 
  1347.              (if braid-p
  1348.              (class-direct-subclasses class)
  1349.              (early-class-direct-subclasses class)))
  1350.            (funcall function class)))
  1351.       (do-class (if (symbolp root)
  1352.             (find-class root)
  1353.             root)))))
  1354.  
  1355. ;;;
  1356. ;;; NOTE: We are assuming a restriction on user code that the method
  1357. ;;;       combination must not change once it is connected to the
  1358. ;;;       generic function.
  1359. ;;;
  1360. ;;;       This has to be legal, because otherwise any kind of method
  1361. ;;;       lookup caching couldn't work.  See this by saying that this
  1362. ;;;       cache, is just a backing cache for the fast cache.  If that
  1363. ;;;       cache is legal, this one must be too.
  1364. ;;;
  1365. ;;; Don't clear this table!  
  1366. (defvar *effective-method-table* (make-hash-table :test 'eq))
  1367.  
  1368. (defun get-secondary-dispatch-function (gf methods types &optional 
  1369.                              method-alist wrappers)
  1370.   (function-funcall (get-secondary-dispatch-function1 
  1371.              gf methods types
  1372.              (not (null method-alist))
  1373.              (not (null wrappers))
  1374.              (not (methods-contain-eql-specializer-p methods)))
  1375.             method-alist wrappers))
  1376.  
  1377. (defun get-secondary-dispatch-function1 (gf methods types method-alist-p wrappers-p
  1378.                         &optional all-applicable-p
  1379.                         (all-sorted-p t) function-p)
  1380.   (if (null methods)
  1381.       #'(lambda (method-alist wrappers)
  1382.       (declare (ignore method-alist wrappers))
  1383.       #'(lambda (&rest args)
  1384.           (apply #'no-applicable-method gf args)))
  1385.       (let* ((key (car methods))
  1386.          (ht-value (or (gethash key *effective-method-table*)
  1387.                (setf (gethash key *effective-method-table*)
  1388.                  (cons nil nil)))))
  1389.     (if (and (null (cdr methods)) all-applicable-p ; the most common case
  1390.          (null method-alist-p) wrappers-p (not function-p))
  1391.         (or (car ht-value)
  1392.         (setf (car ht-value)
  1393.               (get-secondary-dispatch-function2 
  1394.                gf methods types method-alist-p wrappers-p
  1395.                all-applicable-p all-sorted-p function-p)))
  1396.         (let ((akey (list methods
  1397.                   (if all-applicable-p 'all-applicable types)
  1398.                   method-alist-p wrappers-p function-p)))
  1399.           (or (cdr (assoc akey (cdr ht-value) :test #'equal))
  1400.           (let ((value (get-secondary-dispatch-function2 
  1401.                 gf methods types method-alist-p wrappers-p
  1402.                 all-applicable-p all-sorted-p function-p)))
  1403.             (push (cons akey value) (cdr ht-value))
  1404.             value)))))))
  1405.  
  1406. (defun get-secondary-dispatch-function2 (gf methods types method-alist-p wrappers-p
  1407.                         all-applicable-p all-sorted-p function-p)
  1408.   (if (and all-applicable-p all-sorted-p (not function-p))
  1409.       (if (eq *boot-state* 'complete)
  1410.       (let* ((combin (generic-function-method-combination gf))
  1411.          (effective (compute-effective-method gf combin methods)))
  1412.         (make-effective-method-function1 gf effective method-alist-p wrappers-p))
  1413.       (let ((effective (standard-compute-effective-method gf nil methods)))
  1414.         (make-effective-method-function1 gf effective method-alist-p wrappers-p)))
  1415.       (let ((net (generate-discrimination-net 
  1416.           gf methods types all-sorted-p)))
  1417.     (compute-secondary-dispatch-function1 gf net function-p))))
  1418.  
  1419. (defun get-effective-method-function (gf methods &optional method-alist wrappers)
  1420.   (function-funcall (get-secondary-dispatch-function1 gf methods nil 
  1421.                               (not (null method-alist))
  1422.                               (not (null wrappers))
  1423.                               t)
  1424.             method-alist wrappers))
  1425.  
  1426. (defun get-effective-method-function1 (gf methods &optional (sorted-p t))
  1427.   (get-secondary-dispatch-function1 gf methods nil nil nil t sorted-p))
  1428.  
  1429. (defun methods-contain-eql-specializer-p (methods)
  1430.   (and (eq *boot-state* 'complete)
  1431.        (dolist (method methods nil)
  1432.      (when (dolist (spec (method-specializers method) nil)
  1433.          (when (eql-specializer-p spec) (return t)))
  1434.        (return t)))))
  1435.  
  1436. (defun update-dfun (generic-function &optional dfun cache info)
  1437.   (let* ((early-p (early-gf-p generic-function))
  1438.      (gf-name (if early-p
  1439.               (early-gf-name generic-function)
  1440.               (generic-function-name generic-function)))
  1441.      (ocache (gf-dfun-cache generic-function)))
  1442.     (set-dfun generic-function dfun cache info)
  1443.     (let* ((dfun (if early-p
  1444.              (or dfun (make-initial-dfun generic-function))
  1445.              (compute-discriminating-function generic-function)))
  1446.        (info (gf-dfun-info generic-function)))
  1447.       (unless (eq 'default-method-only (type-of info))
  1448.     (setq dfun (doctor-dfun-for-the-debugger 
  1449.             generic-function
  1450.             #+cmu dfun #-cmu (set-function-name dfun gf-name))))
  1451.       (set-funcallable-instance-function generic-function dfun)
  1452.       #+cmu (set-function-name generic-function gf-name)
  1453.       (when (and ocache (not (eq ocache cache))) (free-cache ocache))
  1454.       dfun)))
  1455.  
  1456.  
  1457. (defvar dfun-count nil)
  1458. (defvar dfun-list nil)
  1459. (defvar *minimum-cache-size-to-list*)
  1460.  
  1461. (defun list-dfun (gf)
  1462.   (let* ((sym (type-of (gf-dfun-info gf)))
  1463.      (a (assq sym dfun-list)))
  1464.     (unless a
  1465.       (push (setq a (list sym)) dfun-list))
  1466.     (push (generic-function-name gf) (cdr a))))
  1467.  
  1468. (defun list-all-dfuns ()
  1469.   (setq dfun-list nil)
  1470.   (map-all-generic-functions #'list-dfun)
  1471.   dfun-list)
  1472.  
  1473. (defun list-large-cache (gf)
  1474.   (let* ((sym (type-of (gf-dfun-info gf)))
  1475.      (cache (gf-dfun-cache gf)))
  1476.     (when cache
  1477.       (let ((size (cache-size cache)))
  1478.     (when (>= size *minimum-cache-size-to-list*)
  1479.       (let ((a (assoc size dfun-list)))
  1480.         (unless a
  1481.           (push (setq a (list size)) dfun-list))
  1482.         (push (let ((name (generic-function-name gf)))
  1483.             (if (eq sym 'caching) name (list name sym)))
  1484.           (cdr a))))))))
  1485.  
  1486. (defun list-large-caches (&optional (*minimum-cache-size-to-list* 130))
  1487.   (setq dfun-list nil)
  1488.   (map-all-generic-functions #'list-large-cache)
  1489.   (setq dfun-list (sort dfun-list #'< :key #'car))
  1490.   (mapc #'print dfun-list)
  1491.   (values))
  1492.  
  1493.  
  1494. (defun count-dfun (gf)
  1495.   (let* ((sym (type-of (gf-dfun-info gf)))
  1496.      (cache (gf-dfun-cache gf))
  1497.      (a (assq sym dfun-count)))
  1498.     (unless a
  1499.       (push (setq a (list sym 0 nil)) dfun-count))
  1500.     (incf (cadr a))
  1501.     (when cache
  1502.       (let* ((size (cache-size cache))
  1503.          (b (assoc size (third a))))
  1504.     (unless b 
  1505.       (push (setq b (cons size 0)) (third a)))
  1506.     (incf (cdr b))))))
  1507.  
  1508. (defun count-all-dfuns ()
  1509.   (setq dfun-count (mapcar #'(lambda (type) (list type 0 nil))
  1510.                '(ONE-CLASS TWO-CLASS DEFAULT-METHOD-ONLY
  1511.                  ONE-INDEX N-N CHECKING CACHING 
  1512.                  DISPATCH)))
  1513.   (map-all-generic-functions #'count-dfun)
  1514.   (mapc #'(lambda (type+count+sizes)
  1515.         (setf (third type+count+sizes)
  1516.           (sort (third type+count+sizes) #'< :key #'car)))
  1517.     dfun-count)
  1518.   (mapc #'(lambda (type+count+sizes)
  1519.         (format t "~&There are ~4d dfuns of type ~s"
  1520.             (cadr type+count+sizes) (car type+count+sizes))
  1521.         (format t "~%   ~S~%" (caddr type+count+sizes)))
  1522.     dfun-count)
  1523.   (values))
  1524.  
  1525. (defun gfs-of-type (type)
  1526.   (unless (consp type) (setq type (list type)))
  1527.   (let ((gf-list nil))
  1528.     (map-all-generic-functions #'(lambda (gf)
  1529.                    (when (memq (type-of (gf-dfun-info gf)) type)
  1530.                      (push gf gf-list))))
  1531.     gf-list))
  1532.